//
//  ViewController.swift
//  haolizhijian
//
//  Created by mac on 16/3/26.
//  Copyright © 2016年 Qingke. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    var target = 0 {                  //属性监视器(设置属性之后的操作)
        didSet {
            targetLabel.text = String(target)
        }
    }
    var cycle = 1 {
        didSet {
            cycleLabel.text = String(cycle)
        }
    }
    var totalScore = 0 {
        didSet {
            totalScoreLabel.text = String(totalScore)
        }
    }

    @IBOutlet weak var totalScoreLabel: UILabel!
    @IBOutlet weak var slider: UISlider!
    @IBOutlet weak var targetLabel: UILabel!
    @IBOutlet weak var cycleLabel: UILabel!
    @IBAction func newStartButton(sender: UIButton) {
        target = random()
        cycle = 1
        totalScore = 0
    }
    override func viewDidLoad() {  
        super.viewDidLoad()
        
        
        target = random()
        cycle = 1
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func go(sender: UIButton) {
        
        let score = comScore()
        totalScore += score
        var msg = think(score)
        msg += "\n 你的目标是\(target)\n 你拖动到了\(Int(slider.value))\n 你的成绩是\(score) "
        
        let alert = UIAlertController(title: "你的战绩", message: msg, preferredStyle: .Alert)
        let action = UIAlertAction(title: "再来一次",
                                   style: .Default,
                                   handler: {
                                    action in
                                    self.next()
                                   })
        alert.addAction(action)
        
        presentViewController(alert, animated: true, completion: nil)
    }
    func comScore() ->Int {
        let value = slider.value       //lround转换类型
        
        let wucha = abs(target - Int(value))    //abs绝对值
        let score = 100 - wucha
        return score
    }
    func think(score: Int) ->String{
         var msg = ""
        if score > 95 {
            msg = "太帅了，你击败了\(score)%的人"
        } else if score > 85 {
            msg = "还可以，你击败了\(score)%的人"
        } else {
            msg = "哎呦，你才得了\(score)分"
        }
        return msg
    }
    func next() {
        
        cycle++
        target = random()
    }
    func random() -> Int{
         return Int(arc4random_uniform(100) + 1)
    }
    @IBAction func unwindToHere(segue:UIStoryboardSegue){
    }

}

